package com.github.chirspan.xaas.cfg.web.rest;

import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;
import cn.hutool.db.meta.TableType;
import com.alibaba.fastjson.JSONArray;
import com.github.chirspan.xaas.api.BaseCRUDRestController;
import com.github.chirspan.xaas.core.db.jdbc.DBUtils;
import com.github.chirspan.xaas.core.db.jdbc.JdbcDataProvider;
import com.github.chirspan.xaas.core.db.jdbc.JdbcMetaUtils;
import com.github.chirspan.xaas.core.exception.BizException;
import com.github.chirspan.xaas.core.model.DbDataSource;
import com.github.chirspan.xaas.core.rest.RestResult;
import com.github.chirspan.xaas.core.service.IDatasourceService;
import com.github.chirspan.xaas.core.template.StringTemplate;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;


/**
 * @Author: chenpan
 * @Date: 2021-04-15 16:48:09
 * @Description: 数据源 REST API
 */
@RestController
@RequestMapping("datasource")
public class DatasourceController extends BaseCRUDRestController<DbDataSource> {

    @Autowired
    private IDatasourceService datasourceService;

    @ApiOperation("数据源连接测试")
    @PostMapping("/testDs")
    public RestResult testDatasource(@RequestBody Map<String, Object> datasource) {

        try {
            Connection conn = JdbcDataProvider.getConnection(datasource);
            conn.close();
            return RestResult.OK(null, "连接成功");
        } catch (Exception e) {
            throw new BizException(e);
        }

    }

    private DataSource getDataSource(String dsId) throws Exception {
        DbDataSource dataSource = datasourceService.getDsById(dsId);
        return JdbcDataProvider.getDataSource(dataSource);
    }

    @GetMapping("/rdbms/table")
    public RestResult getTables(@RequestParam("dsId") String dsId) throws Exception {
        DataSource dataSource = getDataSource(dsId);
        List<Table> tables = JdbcMetaUtils.getTablesMeta(dataSource, null, null, TableType.TABLE);
        return RestResult.OK(tables);
    }

    @GetMapping("/rdbms/table/meta")
    public RestResult getTableMetas(@RequestParam("dsId") String dsId, @RequestParam("tableName") String tableName) throws Exception {
        DataSource dataSource = getDataSource(dsId);
        Table table = MetaUtil.getTableMeta(dataSource, tableName);
        return RestResult.OK(table);
    }

    @GetMapping("/meta")
    public RestResult checkAndGetSQLMeta2(@RequestParam("dsId") String dsId, @RequestParam("sql") String sql) {
        try {
            String renderSql = StringTemplate.renderString(sql);
            DataSource dataSource = getDataSource(dsId);
            JSONArray metas = DBUtils.getMetaDataBySql2(dataSource.getConnection(), renderSql);
            return RestResult.OK(metas);
        } catch (Exception e) {
            throw new BizException(e);
        }

    }
}